<!DOCTYPE html>
<html>

<meta http-equiv="content-type" content="text/html;charset=UTF-8" />

<head>
  <meta charset="utf-8">
  <title>layuiAdmin 后台管理员</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
  <link rel="stylesheet" href="../../../layuiadmin/style/public.css">

</head>

<body>
  <div class="layui-card layui-wh100">
    <div class="layui-card-body">
      <div class="layui-form">
        <div class="layui-form-item">
          <div class="layui-collapse" id="menu_box">

          </div>
        </div>
        <div class="layui-form-item">
          <button  class="layui-btn layui-col-md-offset4" id="savaChange" lay-submit="" lay-filter="savaChange">立即提交</button>
        </div>
      </div>
    </div>
  </div>

  <script src="../../../layuiadmin/layui/layui.js"></script>
  <script>
    layui.config({
      base: '../../../../layuiadmin/' //静态资源所在路径
    }).extend({
      index: 'lib/index', //主入口模块
      _role: 'lib/_role'
    }).use(['_role', 'utilJs'], function (option) {
      var _role = layui._role,     
        $ = layui.$,
        utilJs = layui.utilJs;
        
      _role.init().then(r => {
        let dom = ''
        let menu = utilJs.getQueryStringByName('ids')
        localStorage.setItem('pid', r.data.map(r => r.Id))
        r.data.map(c => {
          dom +=
            `<div class="layui-colla-item">
            <h2 class="layui-colla-title">${c.Name}</h2>
            <div class="layui-colla-content layui-show">
              ${c.Ch.map(ch=>`<input type="checkbox" value="${ch.Id}" name="${utilJs.guid()}" ${menu.indexOf(ch.Id)>=0?'checked':''}  title="${ch.Name}">`).toString().replace(/,/g,"")}
            </div>
            </div>`;
        })
        $('#menu_box').append(dom)
        layui.use(['form', 'element'], r => {
          var form = layui.form;
          form.render()
          form.on('submit(savaChange)', r => {
            var ids = [];
            for (var item in r.field) {
              var id = r.field[item]
              ids.push(id)
            }
            if (ids.length > 0)
              ids = ids.toString() + ',' + localStorage.getItem('pid')
            else
              ids = ids.toString()
            let roleid = utilJs.getQueryStringByName('roleid')
            let post = {
              ids,
              roleid
            }
            _role.updateAduit(post).then(r => {
              utilJs.admin.disabled('#savaChange')
              utilJs.admin.topMsg('操作成功', () => {
                localStorage.setItem('guid', 1);
                utilJs.admin.killIframe()
              })
            }).catch(r => utilJs.admin.disabled('#savaChange'))
          });
        })



      })
    })
  </script>
</body>

</html>
<style>

</style>